package jwdt.popup.actions;

import jwdt.utils.ProjectPropertiesUtils;
import jwdt.wizards.InitProFrameWorkWizard;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;


public class InitProFrameWorkAction implements IObjectActionDelegate {

	private Shell shell;
	
	private ISelection selection;
	
	public static IJavaProject javaProject;
	
	public static ProjectPropertiesUtils projectProperties;
	
	/**
	 * Constructor for Action1.
	 */
	public InitProFrameWorkAction() {
		super();		
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {	
		try {
			//判断选择的项目是不是Java Web项目
			if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) {
				IStructuredSelection ssel = (IStructuredSelection) selection;
				if (ssel.size() == 1)
				{
					Object obj = ssel.getFirstElement();
					if (obj instanceof IJavaProject) {
						javaProject = (IJavaProject) obj;	
												
						String javaProjectLocation = javaProject.getProject().getLocation().toString();
						projectProperties=new ProjectPropertiesUtils(javaProjectLocation);
						
						if(projectProperties.isJavaDynamicWebProject()){
							//如是Java Web项目则显示设置界面
							WizardDialog wizardDialog = new WizardDialog(shell, new InitProFrameWorkWizard(selection));
							wizardDialog.setPageSize(300, 390);
							wizardDialog.open();	
							return;
						}						
					}
				}
			}
		} catch (Exception e) {
			MessageDialog.openError(shell, "错误", e.getMessage());
			return;
		}
		//如不是Java Web项目则显示如下信息
		MessageDialog.openInformation(shell, "提示信息", "请确认您选择的是“Dynamic Web Project”！");		
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
	}

}
